/**
 * 新增菜单
 * @url admin/system/menu/sys/add
 * @description 新增菜单
 */
module.exports = {
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '新增成功' };
		
		// 业务逻辑开始-----------------------------------------------------------
		let {
			name,
			menu_id,
			url = '',
			parent_id = '',
			sort = 0,
			enable = true
		} = data;
		
		// 参数验证
		if (!name) {
			return { code: -1, msg: '菜单名称不能为空' };
		}
		if (!menu_id) {
			return { code: -1, msg: '菜单标识不能为空' };
		}
		
		// 检查菜单ID是否已存在
		let existMenu = await db.collection('opendb-admin-menus')
			.where({ menu_id })
			.get();
		if (existMenu.data.length > 0) {
			return { code: -1, msg: '菜单标识已存在' };
		}
		
		// 创建菜单
		await db.collection('opendb-admin-menus').add({
			name,
			menu_id,
			url,
			parent_id,
			sort,
			enable,
			create_date: Date.now()
		});
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}

